package com.nbpt;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;


import com.nbpt.smarthomelibrary.SmartHub;

import java.util.Timer;
import java.util.TimerTask;

public class zhinengjiaju extends Activity {

    private Thread temperturet;
    private Thread temperturet2;
    private Thread temperturet3;
    private TextView wd;
    private TextView gz;
    SmartHub hub = new SmartHub();
    private ImageView show;
    private final int MSG_DISPLAY_temperature = 0;
    private final int MSG_DISPLAY_temperature2 = 1;
    float temp,light;


    int[] imageID = new int[]
            {
                    R.drawable.f1,
                    R.drawable.f2,
                    R.drawable.f3,
                    R.drawable.f4,
                    R.drawable.f5,
                    R.drawable.f6,
                    R.drawable.f7,
                    R.drawable.f8
            };
    int currentImageId = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhinengjiaju);

        wd = findViewById(R.id.wendu);
        gz =findViewById(R.id.guangzhao);
        show =findViewById(R.id.show);

        temperturet = new Mythread();
        temperturet.start();
        temperturet2 = new Mythread2();
        temperturet2.start();
        temperturet3 = new Mythread3();
        temperturet3.start();


//       temperturet3 = new HomeworkThread();
//       temperturet3.start();

      //  handler.post(temperturet);
    }

//    class myhandler extends  Handler
//    {
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
//        }
//    }

    private Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what==MSG_DISPLAY_temperature)
            {
//                wd.setText("温度为"+String.valueOf(msg.obj)+"℃");
                wd.setText("温度为"+String.valueOf(temp)+"℃");
            }
            if (msg.what==MSG_DISPLAY_temperature2)
            {
                gz.setText("光照为"+String.valueOf(light));
            }
            if (msg.what == 0x1222 && temp>35.0)
            {
                super.handleMessage(msg);
                show.setImageResource(imageID[currentImageId++]);
                if (currentImageId>=7)
                {
                    currentImageId=0;
                }
            }
        }
        //      public void handleMessage(Message msg)
//      {
//          String s = String.valueOf(msg.what);
//          TextView wd = (TextView) findViewById(R.id.wendu);
//          wd.setText(wd.getText()+""+s);
//      }
    };

    public class  Mythread extends Thread
    {
        @Override
        public void run() {
            super.run();
            while (true)
            {
                SystemClock.sleep(1000);

//                float tem=hub.getRoomTemperature();
//                Log.d("Mythread","当前温度"+tem);
//                String t = String.valueOf(tem);
//                wd.setText("温度为"+tem+"℃");

                Message msg = Message.obtain();
                msg.what = MSG_DISPLAY_temperature;
//                msg.obj = hub.getRoomTemperature();
                temp = hub.getRoomTemperature();
                handler.sendMessage(msg);;
            }
        }
    }

    public class Mythread2 extends Thread
    {
        @Override
        public void run() {
            super.run();
            while (true)
            {

                SystemClock.sleep(1000);
                Message msg = Message.obtain();
                msg.what = MSG_DISPLAY_temperature2;
//                msg.obj = hub.getRoomIllumination();
                light = hub.getRoomIllumination();
                handler.sendMessage(msg);
            }
        }
    }

    public class Mythread3 extends Thread
    {
        @Override
        public void run() {
            super.run();
            while (true)
            {
                Message msg = new Message();
                msg.what=0x1222;
                handler.sendMessage(msg);
            }
        }
    }

//    public  class HomeworkThread extends Thread
//    {
//        @Override
//        public void run() {
//            super.run();
//            while (true)
//            {
//                SystemClock.sleep(1000);
//                Log.d("HomeworkThread","HomeworkThread正在运行");
//            }
//        }
//    }

}
